import { carTypePage, insertOrUpdateCarType, delCarType } from '../services/line';
import React from 'react';
import { Input, message } from 'antd';


/**
 * 客户数据模型
 */

//表格列表信息
const columns = [
  {
    title: '项目简称',
    dataIndex: 'customerName',
    width: '160px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '车型',
    dataIndex: 'carType',
    width: '150px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '限制',
    dataIndex: 'impose',
    width: '150px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '单位',
    dataIndex: 'imposeUnit',
    width: '150px',
    editable: true,
    ellipsis: true,
  },
];

// 搜索框字段
const searchFields = [
  {
    cols: [
      {
        md: 6,
        label: '项目简称',
        name: 'customerName',
        component: <Input placeholder="请输入客户名称" />,
      },]
  },
];

// 校验规则
const requiredCols = {
  项目简称: [
    {
      required: 'true',
      message: `请输入项目简称!`,
    },
  ],
  车型: [
    {
      required: 'true',
      message: `请输入load号!`,
    },
  ],
};
export default {
  namespace: 'carType',

  state: {
    searchFields: searchFields, // 搜索框信息
    searchValues: {}, // 搜索框里的值
    pageList: [], // 表格数据
    pagination: {
      pageSize: 10,
      current: 1,
      size: 'small',
      showSizeChanger: true,
      showQuickJumper: true,
    }, // 表格分布数据
    columns: columns, // 表格列信息
    editId: '', // 编辑行id
    selectionIds: [], // 表格选择ID
    selectedRows: [], // 表格选择行
    requiredCols: requiredCols, // 字段校验规则
  },

  effects: {
    // 获取分页数据
    *page({ payload }, { call, put }) {
      const res = yield call(carTypePage, payload);
      yield put({
        type: 'page_r',
        payload: res,
      });
    },
    *save({ payload }, { call, put }) {
      const res = yield call(insertOrUpdateCarType, payload);
      yield put({
        type: 'save_r',
        payload: res,
      });
    },
    *del({ payload }, { call, put }) {
      const res = yield call(delCarType, payload);
      yield put({
        type: 'del_r',
        payload: res,
      });
    },
  },

  reducers: {
    // 新增
    handleAdd(state) {
      const { pageList, pagination } = state;
      let addList = pageList.filter(page => {
        return page.id === 0;
      });
      if (addList.length) {
        message.warning('请提交后再添加！');
        return {
          ...state,
        };
      } else {
        pageList.unshift({ orderId: 0, status: 'ENTRY' });
        const newPagination = {
          pages: pagination.pages,
          pageSize: pagination.pageSize,
          total: pagination.total + 1,
          current: pagination.current,
          size: 'small',
          showSizeChanger: true,
          showQuickJumper: true,
        };
        return {
          ...state,
          pageList: pageList,
          pagination: newPagination,
        };
      }
    },
    // 绑定搜索框数据
    handleSearchChange(state, action) {
      const { searchValues } = state;
      const newSearchForm = {
        ...searchValues,
        ...action.payload,
      };
      return {
        ...state,
        searchValues: newSearchForm,
      };
    },
    // 处理编辑
    handleEdit(state, action) {
      return {
        ...state,
        editId: action.payload,
      };
    },

    // 改变分页插件
    handlePagination(state, action) {
      const { payload } = action;
      const { pagination } = state;
      let newPagination = {
        ...pagination,
        ...payload,
      };
      return {
        ...state,
        pagination: newPagination,
      };
    },

    // 表格选中Id
    handleSelection(state, action) {
      const { selectedRowKeys, selectedRows } = action.payload;
      return {
        ...state,
        selectionIds: selectedRowKeys,
        selectedRows: selectedRows,
      };
    },
    // 分页数据请求回来后处理
    page_r(state, action) {
      const { payload } = action;
      if (payload && payload.code === 0) {
        const list = payload.result.records;
        const pagination = {
          pages: payload.result.pages,
          pageSize: payload.result.size,
          total: payload.result.total,
          current: payload.result.current,
          size: 'small',
          showSizeChanger: true,
          showQuickJumper: true,
        };
        return {
          ...state,
          pagination: pagination,
          pageList: list,
          editId: '',
          selectionIds: [],
          selectedRows: [],
        };
      } else {
        return {
          ...state,
        };
      }
    },
    // 处理保存
    save_r(state, action) {
      const { payload } = action;
      if (payload && payload.code === 0) {
        message.success('保存成功！');
      } else {
        message.error(`${payload.errorMsg}` ? `${payload.errorMsg}` : '保存失败！');
      }
      return {
        ...state,
      };
    },
    // 处理删除
    del_r(state, action) {
      const { payload } = action;
      if (payload && payload.code === 0) {
        message.success('删除成功！');
      } else {
        message.error(`${payload.errorMsg}` ? `${payload.errorMsg}` : '删除失败！');
      }
      return {
        ...state,
      };
    },
  },
};
